let { lessee } = require('../models/lesseeModel');
const { moveFiles, removeFiles } = require("../utils/upload");
// 注册
module.exports.register =async (req, res) => {
    let { username, password } = req.body;
    console.log(11,username,req.body)
    let data = await lessee.create({ username, password,money:0,status:1,identity:0});
         if(data._id){
             res.send({
                 status:1,
                 msg:'账号注册成功'
             })
         }else{
             res.send({
                  status:0,
                  msg:'注册失败'
             })
         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

}
// 登录
module.exports.login =async (req, res) => {
    let { username, password } = req.body;
    let data = await lessee.find({ username, password });
   
         if(data.length){
             res.send({
                 status:1,
                 msg:'登录成功',
                 username: data[0]
             })
         }else{
             res.send({
                  status:0,
                  msg:'登录失败'
             })
         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

}
// 渲染
module.exports.getData =async (req, res) => {  
    let data = await lessee.find();
         if(data.length){
             res.send({
                 status:1,
                 msg:'数据获取成功',
                 data
              
             })
         }else{
             res.send({
                  status:0,
                  msg:'数据获取失败'
             })
         }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

}
// 删除
module.exports.del=async(req,res)=>{
          let {_id} =req.body;
        let data=  await lessee.deleteOne({_id});
        if(data.deletedCount){
             res.send({
                 msg:'修改成功',
                  status:1
             })
        }else{
             res.send({
                  msg:'修改失败',
                  status:0
             })
        }
}
//身份验证
module.exports.auth=async(req,res)=>{
       console.log(req.body);
      let {money,telephone,name,_id,imageName} =req.body;
      let data = await lessee.updateOne({_id},{money,telephone,name,imageName,identity:1});
      console.log('操作',data)

      if (imageName) {
          console.log(8888,imageName)
        moveFiles({
            fromPath: "./public/temp",
            toPath: "./public/images",
            filename:  imageName
        })
        removeFiles("./public/temp");
    }
     if(data.nModified==1){
          res.send({
              msg:'发送后台成功',
              status:1
               
          })
     }else{
         res.send({
             msg:'发送后台失败'
         })
     }

}
//是否身份验证 --得到一个人信息
module.exports.isAuth=async(req,res)=>{
   console.log('query',req.query);
   let {_id} =  req.query;
   let data= await lessee.find({_id});
   if(data.length){
        res.send({
            rows: data[0],
            status:1,
            msg:'单个数据获取成功'
        })
   }else{
    res.send({
        rows: data[0],
        status:1,
        msg:'单个数据获取失败'
    })
   }
}
// 修改身份验证状态值
module.exports.identity=async(req,res)=>{
    
       let{_id,identity} =req.body;
       console.log('dentity',identity)
       let data =await lessee.updateOne({_id},{identity});
      
       if(data.nModified==1){
            res.send({
                msg:`认证成功,状态值为${identity}`,
                status:1
            })
       }else{
         res.send({
              msg:'修改失败'
         })
       }
}
// 充值金额
module.exports.pay=async (req,res)=>{
      let{_id,money} = req.body;
     let data =  await lessee.updateOne({_id},{money});
     if(data.nModified==1){
          res.send({
              msg:'充值成功',
              status:1
          })
     }else{
        res.send({
            msg:'充值失败'
        })
     }
}